package cn.oms.manage.controller;

import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.web.JsonResult;
import cn.oms.manage.pojo.param.MedicineAddNewParam;
import cn.oms.manage.pojo.param.MedicineCategoryAddNewParam;
import cn.oms.manage.pojo.param.MedicineCategoryUpdateParam;
import cn.oms.manage.pojo.param.MedicineUpdateParam;
import cn.oms.manage.pojo.vo.MedicineCategoryListVO;
import cn.oms.manage.pojo.vo.MedicineCategoryStandardVO;
import cn.oms.manage.pojo.vo.MedicineListVO;
import cn.oms.manage.pojo.vo.MedicineStandardVO;
import cn.oms.manage.service.IMedicineCategoryService;
import cn.oms.manage.service.IMedicineService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 说明：
 *
 * @author：ChenJJ
 * @date: 2023/6/16
 **/
@Slf4j
@RestController
@RequestMapping("/medicines-category")
@Validated
@Api(tags = "05. 药品类别管理模块")
public class MedicineCategoryController {

    @Autowired
    private IMedicineCategoryService medicineService;

    public MedicineCategoryController() {
        log.debug("创建控制器类对象：AlbumController");
    }

    // http://localhost:9999/medicines/add-new
    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/medicines-category/add-new')")
    @ApiOperation("添加药品类别")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid MedicineCategoryAddNewParam medicineAddNewParam) {
        log.debug("开始处理【添加药品类别】的请求，参数：{}", medicineAddNewParam);
        medicineService.addNew(medicineAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9999/medicines/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/medicines-category/delete')")
    @ApiOperation("根据ID删除药品类别")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "药品类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的药品类别ID值！") Long id) {
        log.debug("开始处理【根据ID删除药品类别】的请求，参数：{}", id);
        medicineService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9999/medicines/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/medicines-category/update')")
    @ApiOperation("修改药品类别详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "药品类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的药品类别ID值！") Long id,
                                           @Valid MedicineCategoryUpdateParam medicineUpdateParam) {
        log.debug("开始处理【修改药品类别详情】的请求，ID：{}，新数据：{}", id, medicineUpdateParam);
        medicineService.updateInfoById(id, medicineUpdateParam);
        return JsonResult.ok();
    }

    // http://localhost:9999/medicines/9527
    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/medicines-category/read')")
    @ApiOperation("根据ID查询药品类别详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "药品类别ID", required = true, dataType = "long")
    })
    public JsonResult<MedicineCategoryStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的药品类别ID值！") Long id) {
        log.debug("开始处理【根据ID查询药品类别详情】的请求，参数：{}", id);
        MedicineCategoryStandardVO queryResult = medicineService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9999/medicines
    @GetMapping("")
    @PreAuthorize("hasAuthority('/medicines-category/read')")
    @ApiOperation("查询药品类别列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult<PageData<MedicineCategoryListVO>> list(@Range(min = 1, message = "请提交有效的页码值！") Integer page,
                                                                  String queryType) {
        log.debug("开始处理【查询属性模板列表】的请求，查询类型：{}, 页码：{}", queryType, page);
        Integer pageNum = page == null ? 1 : page;
        PageData<MedicineCategoryListVO> pageData;
        if ("all".equals(queryType)) {
            pageData = medicineService.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = medicineService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }

}
